package com.xiangban.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.xiangban.aop.Permission;
import com.xiangban.exception.BizException;
import com.xiangban.model.Relation;
import com.xiangban.model.res.ApiResult;
import com.xiangban.model.res.ApiResultCode;
import com.xiangban.model.res.Ids;
import com.xiangban.service.RelationService;
import com.xiangban.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/12/6 15:14
 */
@Slf4j
@RestController
@RequestMapping("/relation")
@Api(tags = "患者管理")
public class RelationController {


    @Autowired
    private RelationService relationService;
    @Autowired
    private CommonUtils commonUtils;

    @RequestMapping("/saveRelation")
    @ApiOperation("添加患者")
//    @Permission(permission = "admin:relation:saveRelation")
    public ApiResult<Object> saveRelation(@RequestBody Relation relation) throws Exception {
        relationService.saveRelation(relation);
        return ApiResult.newSuccess();
    }

    @RequestMapping("/removeRelation")
    @ApiOperation("删除患者")
    @Permission(permission = "admin:relation:removeRelation")
    public ApiResult<Object> removeRelation(@RequestBody Ids ids){
        relationService.removeRelation(ids);
        return ApiResult.newSuccess();
    }

    @RequestMapping("/updateRelation")
    @ApiOperation("修改患者")
    @Permission(permission = "admin:relation:updateRelation")
    public ApiResult<Object> updateRelation(@RequestBody Relation relation){
        relationService.updateRelation(relation);
        return ApiResult.newSuccess();
    }

    @RequestMapping("/getRelation")
    @ApiOperation("详情患者")
    @Permission(permission = "admin:relation:getRelation")
    public ApiResult<Object> getRelation(@RequestBody Relation relation){
        return ApiResult.newSuccess(relationService.getRelation(relation));
    }

    @RequestMapping("/listRelationPage")
    @ApiOperation("列表患者")
    @Permission(permission = "admin:relation:listRelationPage")
    public ApiResult<Object> listRelationPage(@RequestBody Relation relation){
        return ApiResult.newSuccess(relationService.listRelationPage(relation));
    }

    @RequestMapping("/uploadExcel")
    @ApiOperation("患者导入")
    @Permission(permission = "admin:relation:uploadExcel")
    public ApiResult<Object> uploadExcel(MultipartFile file) throws IOException {

        EasyExcel.read(file.getInputStream(), Relation.class, new AnalysisEventListener<Relation>() {
            private static final int BATCH_COUNT = 3000;
            List<Relation> list = new ArrayList<Relation>();

            @SneakyThrows
            @Override
            public void invoke(Relation relation, AnalysisContext analysisContext) {
                Boolean aBoolean = commonUtils.checkIdCard(relation.getIdCard(),relation.getName());
                if (!aBoolean){
                    log.info("身份证姓名校验失败请检查::"+relation.getName());
                }
                list.add(relation);
                if (list.size() >= BATCH_COUNT) {
                    list.clear();
                }
            }

            @SneakyThrows
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                saveData();
            }

            private void saveData() throws Exception {
                for (Relation relation : list) {

                    relationService.saveRelation(relation);
                }

            }
        }).sheet().doRead();
        return ApiResult.newSuccess();
    }



}
